﻿Public Class NCC_Danhsach
    Private nccBL As NhaCungCapBL
    Private nccDT As NhaCungCapDT

    Private Sub loadDGV(ByVal dieukien As String)
        dgv.ClearSelection()
        nccBL = New NhaCungCapBL()
        Dim list As New List(Of NhaCungCapDT)
        list = nccBL.GetData(dieukien)
        dgv.DataSource = list
    End Sub
    Private Sub sua()
        If dgv.SelectedRows.Count > 0 Then
            Dim s As New NCC_Nhap()
            s.flagIsUp = False
            s.MaNCC = dgv.SelectedRows(0).Cells(0).Value
            s.hotenNCC = dgv.SelectedRows(0).Cells(1).Value
            s.dienthoai = dgv.SelectedRows(0).Cells(2).Value
            s.diachi = dgv.SelectedRows(0).Cells(3).Value
            s.email = dgv.SelectedRows(0).Cells(4).Value
            s.ShowDialog()
            loadDGV("")
        Else
            Return
        End If
    End Sub

    Private Sub NCC_Danhsach_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        loadDGV("")
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim t As New NCC_Nhap()
        t.flagIsUp = True
        t.ShowDialog()
        loadDGV("")
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        If dgv.SelectedRows.Count > 0 Then
            If MessageBox.Show("Bạn có chắc muốn xóa nhà cung cấp <" + dgv.SelectedRows(0).Cells(1).Value.ToString() + "> nccông ?", "Xóa thông báo", MessageBoxButtons.YesNo) = MsgBoxResult.Yes Then
                nccDT = New NhaCungCapDT(Convert.ToInt32(dgv.SelectedRows(0).Cells(5).Value))
                nccBL = New NhaCungCapBL()
                nccBL.Delete(nccDT)
                MessageBox.Show("Xóa thành công")
                loadDGV("")
            Else
                MsgBox("Đã hủy thao tác xóa!", vbExclamation, "Thông báo")
            End If
        End If
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        sua()
    End Sub

    Private Sub btnAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAll.Click
        loadDGV("")
    End Sub

    Private Sub txtTim_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtTim.TextChanged
        loadDGV(" SELECT * FROM NhaCungCap Where hotenNCC LIKE N'%" + txtTim.Text + "%' OR MaNCC LIKE '%" + txtTim.Text + "%'")
    End Sub

    Private Sub txtTim_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtTim.KeyDown
        If e.KeyCode = Keys.Enter Then
            loadDGV(" SELECT * FROM NhaCungCap Where hotenNCC LIKE N'%" + txtTim.Text + "%' OR MaNCC LIKE '%" + txtTim.Text + "%'")
        End If
    End Sub

    Private Sub dgv_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgv.DoubleClick
        sua()
    End Sub
End Class